package logic

import (
	"context"

	"go-zero_try/mall/order/rpc/pb/internal/svc"
	"go-zero_try/mall/order/rpc/pb/pb"
	"go-zero_try/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type ModifyLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewModifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ModifyLogic {
	return &ModifyLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ModifyLogic) Modify(in *pb.ModAndRgsReq) (*pb.ModAndRgsReps, error) {
	u1, err := l.svcCtx.UserModel.FindOne(l.ctx, in.UID)
	msg := ""
	if err == nil {
		if in.Password == u1.Password {
			u2 := model.Users{
				Username: in.Name,
				Password: in.Password,
				Age:      in.Age,
				Uid:      in.UID,
			}
			err := l.svcCtx.UserModel.Update(l.ctx, &u2)
			if err == nil {
				msg = "修改成功"
			} else {
				msg = "修改失败"
			}
		} else {
			msg = "密码错误"
		}
	} else {
		msg = "查无此人"
	}
	return &pb.ModAndRgsReps{
		Msg:      msg,
		UID:      in.UID,
		Name:     in.Name,
		Password: in.Password,
		Age:      in.Age,
	}, nil
}
